<?php
class Ticket_model extends CI_Model {
	function getAll() {
		$query = $this->db->get ( 'ticket' );
		return $query->result ( 'Ticket' );
	}
	
	function get_id($info) {
		$first = $info ['first'];
		$last = $info ['last'];
		$creditcardnumber = $info ['creditcardnumber'];
		$creditcardexpiration = $info['creditcardexpiration'];
		$showtime_id = $info['showtime'];
		$seat = $info['seat'];
	
		$sql = $this->db->query("SELECT ticket as 'ticket' FROM ticket where first = '$first' and last = '$last' and creditcardnumber = '$creditcardnumber'
				and creditcardexpiration = '$creditcardexpiration' and showtime_id = $showtime_id and seat = $seat");
		return $sql->row();
	}
	
	function get($id) {
		$query = $this->db->query("select m.title as 'title', th.name as 'name', 
				t.first as 'first', t.last as 'last', t.creditcardnumber as 'creditcardnumber',
				t.creditcardexpiration as 'creditcardexpiration', t.seat as 'seat',
				t.ticket as 'ticket'
				from movie m, theater th, showtime s, ticket t
				where t.showtime_id = s.id and s.movie_id = m.id and
				s.theater_id = th.id and t.ticket = '$id'");
		return $query->row();
	}
	
	function delete($id) {
		return $this->db->delete ( 'ticket', array (
				'id' => $id 
		) );
	}
	
	function insert($info) {
		$first = $info ['first'];
		$last = $info ['last'];
		$creditcardnumber = $info ['creditcardnumber'];
		$creditcardexpiration = $info['creditcardexpiration'];
		$showtime_id = $info['showtime'];
		$seat = $info['seat'];
		
		$this->db->query("INSERT INTO ticket(first, last, creditcardnumber, creditcardexpiration, showtime_id, seat) VALUES('$first', '$last','$creditcardnumber', '$creditcardexpiration', $showtime_id, $seat)");
	}
	
	function update($ticket) {
		$this->db->where ( 'id', $ticket->id );
		return $this->db->update ( 'ticket', array (
				'first' => $ticket->first,
				'last' => $ticket->last,
				'ccnumber' => $ticket->ccnumber,
				'ccexpiration' => $ticket->ccexpiration,
				'showtime_id' => $ticket->shotime_id,
				'seat' => $ticket->seat 
		) );
	}
	
	function get_tickets() {
		$query = $this->db->query("select t.ticket as 'ticket', m.title as 'title', th.name as 'name', 
				t.seat as 'seat', t.first as 'first', t.last as 'last', 
				t.creditcardnumber as 'creditcardnumber', 
				t.creditcardexpiration as 'creditcardexpiration'
			from movie m, theater th, showtime s, ticket t
			where t.showtime_id = s.id and s.movie_id = m.id and 
				s.theater_id = th.id
			order by t.ticket");
		return $query->result();
	}
	
	function get_seats($id) {
		$query = $this->db->query("select t.ticket as 'id', t.seat as 'seat'
			from movie m, theater th, showtime s, ticket t
			where t.showtime_id = s.id and s.movie_id = m.id and
				s.theater_id = th.id and s.id = '$id'");
		return $query->result();
	}
}